import axios from 'axios';
import { showFailToast } from 'vant';
import router from '../router';
// hhtp://192.271.32.1:3000   /user
axios.defaults.baseURL = 'http://backend-api-01.newbee.ltd/api/v1'   //前半段的ip
axios.defaults.withCredentials = true   //允许axios在请求的过程中携带cookie凭证
axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest'  //开启axios在浏览器中的能力
axios.defaults.headers['token'] = localStorage.getItem('token') || ''  //去浏览器本地存储取token
axios.defaults.headers.post['Content-Type'] = 'application/json'  //post请求转为json

//拦截后端返回的响应数据
axios.interceptors.response.use(res => {
  if (typeof res.data != 'object') {
    showFailToast('服务器异常');
    return Promise.reject(res)
  }
  if (res.data.resultCode != 200) {
    if (res.data.message) {
      return showFailToast(res.data.message);
    }
    if (res.data.resultCode===416) {  //登录失效
      router.push('/login')   
    }
    return Promise.reject(res.data)
  }
  return res.data  //正常情况
})

export default axios